//
//  DownloadingOpCell.swift
//  GhostWorld
//
//  Created by Kam on 2018/6/29.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit
import Reachability

protocol DownloadingOpCellDelegate {
  func downloadAll()
  func pauseAllDownload()
  func removeAllDownload()
}

class DownloadingOpCell: UITableViewCell {
  
  enum DownloadType {
    case downloadAll, pauseAll
  }
  
  // MARK: - IBOutlets
  
  @IBOutlet private weak var downLoadButton: UIButton!
  
  // MARK: - Private vars
  
  private var downloadType: DownloadType = .pauseAll {
    didSet {
      switch downloadType {
      case .pauseAll:
        downLoadButton.setImage(UIImage(named: "PauseAllDownloadButton"), for: .normal)
      case .downloadAll:
        downLoadButton.setImage(UIImage(named: "DownloadAllButton"), for: .normal)
      }
    }
  }
  
  // MARK: - Private methods
  
  
  
  // MARK: - Public vars
  
  var delegate: DownloadingOpCellDelegate?
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    if Reachability()?.connection == .wifi {
      downloadType = .pauseAll
    } else {
      downloadType = .downloadAll
    }
  }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state
  }
  
  // MARK: - IBActions
  
  @IBAction func downloadPause() {
    if downloadType == .pauseAll {
      delegate?.pauseAllDownload()
      downloadType = .downloadAll
    } else {
      delegate?.downloadAll()
      downloadType = .pauseAll
    }
  }
  
  @IBAction func removeAllDownload() {
    delegate?.removeAllDownload()
  }
}
